package com.atguigu.gulimall.discount.service.impl;

import com.atguigu.feign.entity.MemberPrice;
import com.atguigu.feign.to.SkuReductionTo;
import com.atguigu.gulimall.discount.entity.MemberPriceEntity;
import com.atguigu.gulimall.discount.entity.SkuLadderEntity;
import com.atguigu.gulimall.discount.service.MemberPriceService;
import com.atguigu.gulimall.discount.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.discount.dao.SkuFullReductionDao;
import com.atguigu.gulimall.discount.entity.SkuFullReductionEntity;
import com.atguigu.gulimall.discount.service.SkuFullReductionService;
import org.springframework.util.CollectionUtils;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    //保存sku 优惠/满减等信息
    @Override
    public void saveSkuReductionTo(SkuReductionTo skuReductionTo) {

        //保存sku 满减信息   满多少 大于0 && 减多少 大于0 才保存满减信息
        if(skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1 && skuReductionTo.getReducePrice().compareTo(new BigDecimal("0")) == 1){
            SkuFullReductionEntity skuFullReduction = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuReductionTo,skuFullReduction);
            skuFullReduction.setAddOther(skuReductionTo.getPriceStatus());
            this.save(skuFullReduction);
        }


        //保存sku 折扣信息   买 大于0件 打大于0折 才保存折扣信息。
        if(skuReductionTo.getFullCount() > 0 && skuReductionTo.getDiscount().compareTo(new BigDecimal("0")) == 1){
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(skuReductionTo,skuLadderEntity);
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }

        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        if(!CollectionUtils.isEmpty(memberPrice)){
            List<MemberPriceEntity> collect = memberPrice.stream().filter(member->{
                return member.getPrice().compareTo(new BigDecimal("0")) == 1;
            }).map(member -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setMemberLevelId(member.getId());
                memberPriceEntity.setMemberLevelName(member.getName());
                memberPriceEntity.setMemberPrice(member.getPrice());
                memberPriceEntity.setAddOther(1);
                memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                return memberPriceEntity;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(collect);
        }
    }

}